import router from "./router";
import store from './store/index'


router.beforeEach((to, from, next)=>{
    let token = store.state.token

    if(token){
        if(to.path == '/login'){
            next(from.path)
        }else{
            if(getRouter(to.path)){
                next()
            }else{
                next('/404')
            }
        }
    }else{
        if(to.path == '/login'){
            next()
        }else{
            next('/login')
        }
    }
})


function getRouter(path){
    // 获取到所有路由
    let allRouter = router.getRoutes();

    let routers = allRouter.filter((item)=>{
        return item.path == path 
    })

    if(routers.length > 0){
        return true
    }else{
        return false
    }
}